package com.datam.filter;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.Filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import com.alibaba.fastjson.JSON;
import com.datam.controller.UserController;
import com.datam.dao.CompanyInfoDAO;
import com.datam.dao.CompanyPkInfoDAO;
import com.datam.dao.YJDAO;
import com.datam.domain.CompanyInfoDO;
import com.datam.domain.CompanyPkInfoDO;
import com.datam.domain.SysUserDO;
import com.datam.utils.HttpClient;
import com.datam.utils.YjInfoUtils;
import com.fasterxml.jackson.annotation.JsonAlias;


@Component
public class SpringScheduled {
	
	private static Logger logger = LoggerFactory.getLogger(UserController.class);

	//@Value("${openurl}")
    private static String url = "http://182.92.185.221/";
    //private String url = "http://150.109.148.212/";

    @Autowired
	CompanyInfoDAO companyInfoDAO;
    
    @Autowired
	CompanyPkInfoDAO companyPkInfoDAO ; 
    
    @Autowired
    YJDAO yJDAO ; 
    
    
    Map<String,String> dwMap = new HashMap<String,String>(){{  
	      put("PFL","毫克/立方米"); 
	      put("ND","毫克/千克"); 
	      put("a25002","苯"); 
	      put("a25003","甲苯");
	      put("a25005","乙苯"); 
	      put("a24088","非甲烷总烃");
	      put("flowvocs","流量,立方米");
	      put("a01013","烟气压力,千帕"); 
	      put("a01012","烟气温度,摄氏度"); 
	      put("a01011","烟气流速,烟气流速");
	      put("a01014","烟气湿度,%"); 
	      put("a01016","烟道截面积,平方米");
	
    }};
	//告警推送
	@Scheduled(cron = "0 0/1 * * * ?")
	//@Scheduled(cron = "0 0 0 * * ?")
	public void pushScheduled(){
		logger.info("告警任务");
		List<Map<String, Object>> pdData = companyInfoDAO.selectPfByType("2051",0);
		logger.info("新增数据"+pdData.size()+"条");
		companyInfoDAO.updatePf();
		List<String> codes = new ArrayList<>();
		for(Map<String, Object> map : pdData){
			if(map.get("code").equals("a24088")){
				//System.out.println("dd");
			}
			String key = map.get("code")+"_"+map.get("mn_id");
			codes.add(key);
			Float value =Float.valueOf(map.get("pfl")+"") ;
			//先统计
			Float f = YjInfoUtils.pfl.get(key);
			if(value.equals(0f))  continue ;
			if(f==null){
				YjInfoUtils.pfl.put(key, value);
				yJDAO.insertPfl(map.get("code")+"",map.get("mn_id")+"",value);
			}else{
				BigDecimal b1 = new BigDecimal(Float.toString(f));
				BigDecimal b2 = new BigDecimal(Float.toString(value));
				f = b1.add(b2).floatValue();
				YjInfoUtils.pfl.put(key, f);
				yJDAO.updatePfl(map.get("mn_id")+"",map.get("code")+"",f);	
			}
			CompanyPkInfoDO pkInfo = YjInfoUtils.pk.get(map.get("mn_id"));
			if("vocs_gray".equals(pkInfo.getIconCls())||pkInfo.getIconCls()==null){
				pkInfo.setIconCls("vocs_green");
				companyPkInfoDAO.updateCompanyPkInfoById(pkInfo);
			}
			yj(key,map,f);
		}
		List<Map> bjdata = companyInfoDAO.selectBj();
		for(Map<String, Object> map : bjdata){
			String key = map.get("code")+"_"+map.get("nm_id");
			Float value =Float.valueOf(map.get("pfl")+"") ;
			if(codes.contains("a25003"+"_"+map.get("mn_id"))||
					codes.contains("a25005"+"_"+map.get("mn_id"))	){
				yj(key,map,value);
			}
		}
		logger.info("结束");
	}
	
	private void yj(String key,Map map,Float f ) {
		try {
			//预警
			Float yjz = YjInfoUtils.yj.get(key);
			if(yjz==null) return ;
			//告警
			if(yjz<f){
				Map m = YjInfoUtils.pkyj.get(key);
				if(m!=null&&"1".equals(m.get("STATE"))){
					CompanyPkInfoDO pkInfo = YjInfoUtils.pk.get(map.get("mn_id"));
					if("vocs_red".equals(pkInfo.getIconCls())){
						logger.info("该排口超标已推送一次，不推送"+pkInfo);
						return;
					}
					pkInfo.setIconCls("vocs_red");
					companyPkInfoDAO.updateCompanyPkInfoById(pkInfo);
					String zzt = YjInfoUtils.zyj.get(pkInfo.getCOMPANYID()+"");
					if(pkInfo==null||"1".equals(zzt)){//预警
						SysUserDO user = YjInfoUtils.yh.get(pkInfo.getCOMPANYID()+"");
						
						CompanyInfoDO com = YjInfoUtils.gs.get(pkInfo.getCOMPANYID()+"");
						MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
						List<String> str = new ArrayList<String>();
						Map pushData = new HashMap<>();
						Map info = new HashMap<>();
						info.put("company",com.getNAME()+"-"+pkInfo.getNAME());
						info.put("ass", dwMap.get(map.get("code")));
						info.put("time", new SimpleDateFormat("yyyy-MM-dd HH").format(new Date()));
						pushData.put("info", info);
						if(user.getWxid()==null){
							logger.info("无推送人员"+info);
						}
						String wxids = user.getWxid() ;
						List<String> opends = JSON.parseArray(wxids, String.class);
						for(String openId : opends){
							pushData.put("openId",openId);
							str.add(JSON.toJSONString(pushData));
							params.add("data", "["+String.join(",", str)+"]");
							logger.info("推送人员"+params);
							try {
								HttpClient.sendPostRequest(url+"notify", params);	
							} catch (Exception e) {
								e.printStackTrace();
							}
							params.clear();

						}
					}
				}
			}
			
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		List<String> str = new ArrayList<String>();
		Map pushData = new HashMap<>();
		MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
		Map info = new HashMap<>();
		info.put("company","文马科技-测试排口");
		info.put("ass", "code");
		info.put("time", new SimpleDateFormat("yyyy-MM-dd HH").format(new Date()));
		pushData.put("info", info);
		pushData.put("openId","oVRJBvxmoOfX3PeWYEBRkM2p39CA");
		str.add(JSON.toJSONString(pushData));
		params.add("data", "["+String.join(",", str)+"]");
		HttpClient.sendPostRequest(url+"notify", params);	

	}
	@Autowired
	 LoadInfo loadInfo ;
	//@Scheduled(cron = "0 0/5 * * * ?")
	//@Scheduled(cron = "0 0 0 * * ?")
	public void sxScheduled(){
		try {
			loadInfo.run();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
